package com.csmz.mybookreadernew.UI.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.csmz.mybookreadernew.DBSQLite.DbMySQLite;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.Utils.Md5Utils;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_main_title;  //标题
    private TextView tv_back;   //返回按钮
    private Button btn_register;    //注册按钮
    //用户名、密码、再次输入的密码的控件
    private EditText et_user_name,et_psw,et_psw_again;
    //用户名、密码、再次输入的密码的空间的获取值
    private String userName,psw,pswAgain;
    //标题布局
    private RelativeLayout rl_title_bar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置页面布局
        setContentView(R.layout.activity_register);
        //设置此页面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        //获取main_title_bar页面布局中的ui控件
        tv_main_title= (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("注册");
        rl_title_bar= (RelativeLayout) findViewById(R.id.rl_title_bar);
//        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        tv_back= (TextView) findViewById(R.id.tv_back);

        //获取activity_register页面布局中的ui控件
        btn_register= (Button) findViewById(R.id.btn_register);
        et_user_name= (EditText) findViewById(R.id.et_user_name);
        et_psw= (EditText) findViewById(R.id.et_psw);
        et_psw_again= (EditText) findViewById(R.id.et_psw_again);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.this.finish();
            }
        });

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入在相应控件中的字符串
                getEditString();
                if (TextUtils.isEmpty(userName)){
                    Toast.makeText(RegisterActivity.this,"请输入用户名", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(psw)){
                    Toast.makeText(RegisterActivity.this,"请输入密码", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(pswAgain)){
                    Toast.makeText(RegisterActivity.this,"请再次输入密码", Toast.LENGTH_SHORT).show();
                }else if (!psw.equals(pswAgain)){
                    Toast.makeText(RegisterActivity.this,"输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                }else if (isExistUserName(userName)){
                    Toast.makeText(RegisterActivity.this,"此账户名已存在", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(RegisterActivity.this,"注册成功", Toast.LENGTH_SHORT).show();
                    //把用户名和密码保存到SharedPreferences中
                    saveRegisterInfo(userName,psw);
                    //注册成功后把用户名传递到LoginActivity.java中
                    Intent data=new Intent();
                    data.putExtra("userName",userName);
                    setResult(RESULT_OK,data);
                    //把信息保存到数据库中
                    DbMySQLite dbMySQLite=new DbMySQLite(RegisterActivity.this);
                    dbMySQLite.addUserInfo(userName,null,null,null);
                    RegisterActivity.this.finish();
                }
            }
        });


    }

    /**
     * 获取控件中的字符串
     */
    public void getEditString() {
        userName=et_user_name.getText().toString().trim();
        psw=et_psw.getText().toString().trim();
        pswAgain=et_psw_again.getText().toString().trim();
    }

    /**
     * 从SharedPreferences中读取输入的用户名，并判断此用户名是否存在
     */
    private boolean isExistUserName(String userName){
        boolean has_userName=false;
        SharedPreferences sp=getSharedPreferences("LoginInfo",MODE_PRIVATE);
        String spPsw=sp.getString(userName,"");
        if (!TextUtils.isEmpty(spPsw)){
            has_userName=true;
        }
        return has_userName;
    }

    /**
     * 保存用户名和密码到SharedPreferences
     */
    private void saveRegisterInfo(String userName, String psw){
        String md5Psw= Md5Utils.md5(psw);  //把密码用MD5加密
        SharedPreferences sp=getSharedPreferences("LoginInfo",MODE_PRIVATE);  //保存到LoginInfo
        SharedPreferences.Editor editor=sp.edit();   //获取编译器
        //以用户名为key，密码为value保存到SharedPreferences中
        editor.putString(userName,md5Psw);
        //Log.d("csmz", "saveRegisterInfo: userName="+userName+"md5Psw="+md5Psw);
        editor.commit();  //提交修改

    }
}
